﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Daycare.Models;
using Daycare.DAL;

namespace Daycare.Services
{
    public class UserService
    {
        public UserService()
        {
            userRepository = new UserRepository();
            employees = userRepository.GetEmployees(false);
        }

        public List<Employee> GetEmployees(bool includeTerminatedEmployees)
        {
            employees = userRepository.GetEmployees(true);

            if (includeTerminatedEmployees == false)
            {
                var currentlyEmployedEmployees = from employee in employees where employee.isEmployed == true select employee;

                return new List<Employee>(currentlyEmployedEmployees);
            }
            else
            {
                return employees;
            }
        }

        public Employee GetEmployee(String userName)
        {
            var emp = from employee in employees where employee.userName == userName select employee;

            if( emp.Count() < 1)
            {
                return null;
            }
            else
            { 
                return (Employee)emp.ElementAt(0);
            }
        }

        public Employee GetEmployee(int employeeId)
        {
            var emp = from employee in employees where employee.employeeId == employeeId select employee;

            if (emp.Count() < 1)
            {
                return null;
            }
            else
            {
                return (Employee)emp.ElementAt(0);
            }
        }

        public bool UpdateEmployee(Employee employee)
        {
            return userRepository.UpdateEmployee(employee);
        }

        public bool AddEmployee(Employee employee, String pwd, String salt)
        {
            return userRepository.AddEmployee(employee, pwd, salt);
        }

        public bool UpdateSalt(String userName, String salt)
        {
            return userRepository.UpdateSalt(userName, salt);
        }

        public bool UpdatePassword(String userName, String password)
        {
            return userRepository.UpdatePassword(userName, password);
        }

        public Password GetPasswordInfo(String userName)
        {
            return userRepository.GetPasswordInfo(userName);
        }

        public String ParsePhoneNumberFromControl(TextBox tbPhoneNumber)
        {
            String phoneNumber = String.Empty;
            if (tbPhoneNumber.Text == String.Empty || tbPhoneNumber.Text == "___-___-____")
            {
                phoneNumber = null;
            }
            else
            {
                phoneNumber = tbPhoneNumber.Text;
            }

            return phoneNumber;
        }

        public String ParseEmailAddressFromControl(TextBox tbEmailAddress)
        {
            String emailAddress = String.Empty; 

            if (tbEmailAddress.Text == String.Empty)
            {
                emailAddress = null;
            }
            else
            {
                emailAddress = tbEmailAddress.Text;
            }

            return emailAddress;
        }

        public String ParseMiddleInitial(TextBox tbMiddleInitial)
        {
            String middleInitial = String.Empty;

            if (tbMiddleInitial.Text == String.Empty)
            {
                middleInitial = null;
            }
            else
            {
                middleInitial = tbMiddleInitial.Text;
            }

            return middleInitial;
        }

        public List<Note> GetEmployeeNotes(Employee employee)
        {
            UserRepository ur = new UserRepository();
            
            return ur.GetEmployeeNotes(employee);
        }

        public bool AddEmployeeNote(Note note)
        {
            userRepository.AddEmployeeNote(note);

            return true;
        }

        private UserRepository userRepository;
        private List<Employee> employees;
    }
}